import { _decorator, Component, Node, Button, Label } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { DailyTaskScrollView } from './DailyTaskScrollView';
import { IntervalStartNow, Timer } from '../../../../scripts/libs/rxjs/cc3/CreatorSchedule';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { TaskItem } from './TaskItem';
import { EventTarget } from 'cc';
import { taskm } from '../../../../scripts/managers/TaskManager';
import { ChestOpenPanel } from '../ChestOpenPanel/ChestOpenPanel';
import { gm } from '../../../../scripts/managers/GameManager';
import { udm } from '../../../../scripts/managers/UserDataManager';
import { lm } from '../../LobbyManager';
import { MSResolve, MSRToString_th_m_s } from '../../../../scripts/libs/utils/date';
import { Vec3 } from 'cc';
import { v3 } from 'cc';
import { forkJoin, Observable } from 'rxjs';
import { TweenToStart } from '../../../../scripts/libs/rxjs/cc3/TweenStart';
const { ccclass, property } = _decorator;

@ccclass('DailyTaskPanel')
export class DailyTaskPanel extends BasePanel {

    @property(Node)
    mainNode: Node = null;

    @property(DailyTaskScrollView)
    dailyTaskScrollView: DailyTaskScrollView = null;

    @property(Button)
    closeButton: Button = null;

    @property(TaskItem)
    chestTaskItem: TaskItem = null;

    @property(Label)
    remainLabel: Label = null;

    eventTarget: EventTarget = new EventTarget();

    willBind(): void {
        this.chestTaskItem.taskId.value = 100;
        this.dailyTaskScrollView.eventTarget = this.eventTarget;
        lm.dailyTaskPanelOpened.value = true;
        // this.adaptive();
        this.mainNode.setScale(.5, .5, .5);
    }

    useObserves() {
        return [
            IntervalStartNow(1, this.node).subscribe(this.refresh.bind(this)),
        ];
    }

    // adaptive() {
    //     this.mainNode.setScale(v3(Vec3.ONE).multiplyScalar(lm.getAdaptiveScale()));
    // }
    
    openPanelAnim(): Observable<any> {
        return forkJoin([
            TweenToStart(this.ensureUIOpacity(), .2, { opacity: 255 }, { easing: 'expoOut' }),
            TweenToStart(this.mainNode, .4, { scale: lm.getAdaptiveScaleV3() }, { easing: 'backOut' }),
        ]);
    }

    closePanelAnim(): Observable<any> {
        this.ensureUIOpacity();
        return forkJoin([
            TweenToStart(this.ensureUIOpacity(), .2, { opacity: 0 }, { easing: 'expoIn' }),
            TweenToStart(this.mainNode, .2, { scale: v3(.5, .5, .5) }, { easing: 'backIn' }),
        ]);
    }


    refresh() {
        let ms = udm.getRemainMsToNextDay();
        let msr = MSResolve(ms);
        // this.remainLabel.string = `Reset: ${MSRToString_th_m_s(msr)}`;
        this.remainLabel.string = `${MSRToString_th_m_s(msr)}`;
    }

    onLoadView(): void {
        this.eventTarget.on('claim', this.onItemClaim.bind(this), this);
        this.eventTarget.on('go', this.onItemGo.bind(this), this);
    }

    onDestroyView(): void {
        this.eventTarget.removeAll(this);
        lm.dailyTaskPanelOpened.value = false;
    }

    start() {
        Timer(.1, this.node).subscribe(_ => {
            this.dailyTaskScrollView.openScrollView();
        }).addTo(this.node);

        BindButton(this.closeButton).subscribe(this.closeButtonClicked.bind(this))
    }

    closeButtonClicked() {
        if (this.waitingChest) return;
        DailyTaskPanel.CloseProcess(null, 'close');
    }

    waitingChest = false;
    onItemClaim(taskItem: TaskItem) {
        this.log('@onItemClaim', taskItem);

        // 总保箱任务
        let taskInfo100 = taskm.taskMap.get(100);
        if (taskm.getTaskCurrentTimes(100) >= taskInfo100.times) { // 总保箱任务自动开启
            taskm.claim(100);
        }

        if (udm.stageChest.value != null) {
            this.waitingChest = true;
            Timer(1, this.node).subscribe(_ => {
                ChestOpenPanel.StartProcess((panel: ChestOpenPanel) => {
                    panel.stageInfo.value = gm.stageInfo.value;
                    panel.key.value = udm.stageChest.value;
                }).subscribe(_ => {
                    this.waitingChest = false;
                });
            }).addTo(this.node);
        }
    }

    onItemGo() {
        this.log('@onItemGo', arguments);
        DailyTaskPanel.CloseProcess(null, 'go_close');
    }

}

